package com.ruoyi.project.datum.specification.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.datum.series.domain.Series;
import com.ruoyi.project.datum.specification.domain.Specification;
import com.ruoyi.project.datum.specification.mapper.SpecificationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 描述：产品规格信息 业务处理层
 * 时间：2020/5/18
 * 作者：WangHaiDong
 */
@Service
public class SpecficationServiceImpl implements ISpecificationService {

    @Autowired
    private SpecificationMapper specificationMapper;

    /**
     * 根据条件查询产品规格信息
     * @param specification
     * @return
     */
    @Override
    public List<Specification> selectAll(Specification specification) {
        return specificationMapper.selectAll(specification);
    }

    @Override
    public String checkSpeNameUnique(Specification specification) {
        // 判断ID是否为空
        Long SpecificationId = StringUtils.isNull(specification.getSpeId()) ? -1L : specification.getSpeId();
        // 根据产品系列名称查询是否有数据
        Specification info = specificationMapper.checkSpeNameUnique(specification.getSpeName());
        // 判断产品系列是否又数据 和 前后的两次ID值是否相同
        if (StringUtils.isNotNull(info) && info.getSpeId().longValue() != SpecificationId.longValue())
        {
            return UserConstants.SPE_NAME_NOT_UNIQUE;
        }
        return UserConstants.SPE_NAME_UNIQUE;
    }

    /**
     * 添加产品规格信息
     * @param specification
     * @return
     */
    @Override
    public int insertSpecification(Specification specification) {
        return specificationMapper.insertSpecification(specification);
    }

    /**
     * 根据产品规格ID 批量删除产品规格信息
     * @param ids
     * @return
     */
    @Override
    public int deleteSpecificationByIds(String ids) {
        Long[] speIds = Convert.toLongArray(ids);

        return specificationMapper.deleteSpecificationByIds(speIds);
    }

    /**
     * 根据产品规格ID 查询产品规格信息
     * @param speId
     * @return
     */
    @Override
    public Specification selectSpecificationById(Long speId) {
        return specificationMapper.selectSpecificationById(speId);
    }

    /**
     * 根据产品规格ID 修改产品规格信息
     * @param specification
     * @return
     */
    @Override
    public int updateSpecification(Specification specification) {
        return specificationMapper.updateSpecification(specification);
    }
}
